<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String dept_id = request.getParameter("dept_id") == null ? "" : request.getParameter("dept_id");
	String dept_name = request.getParameter("dept_name") == null ? "" : request.getParameter("dept_name");
	String gd_code = request.getParameter("gd_code") == null ? "" : request.getParameter("gd_code");
	String gd_name = request.getParameter("gd_name") == null ? "" : request.getParameter("gd_name");
	String customer_id = request.getParameter("customer_id") == null ? "" : request.getParameter("customer_id");
	String customer = request.getParameter("customer") == null ? "" : request.getParameter("customer");
	String return_id = request.getParameter("return_id") == null ? "" : request.getParameter("return_id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(return_id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建工余库还料":"编辑工余库还料" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		$(function(){
			if('<%=flag%>' == 1){
				$('#ff').form('load','<%=contextPath %>/springMVC/gd/getCustomerReturnById?return_id=<%=return_id%>');
			}
			$('#table').datagrid({
				<%=isAdd?"url:'',":"url:'"+contextPath+"/springMVC/gd/listGDCustomerReturnDetail?return_id="+return_id+"',"%>
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'借料明细id',field:'borrow_detail_id',hidden:true}
				]],
				columns:[[
					{title:'借料日期',field:'operation_date',width:100,align:'center'},
		            {title:'借料摘要',field:'summary',width:200,align:'center'},
					{title:'仓库',field:'warehouse_name',width:100,align:'center'},
					{title:'产品编号',field:'code',width:100,align:'center'},
					{title:'产品名称',field:'name',width:100,align:'center'},
					{title:'规格/型号',field:'format_type',width:150,align:'center'},
					{title:'单位',field:'unit_name',width:50,align:'center'},
					{title:'借料数量',field:'borrow_num',width:100,align:'center'},
					{title:'已还数量',field:'already_return_num',width:100,align:'center'},
					{title:'还料数量',field:'return_num',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							min:0,
							precision:2
						}
					}},
					{title:'已还金额',field:'already_return_money',width:100,align:'center'},
					{title:'还钱',field:'return_money',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							min:0,
							precision:2
						}
					}},
					{title:'备注',field:'remark',width:100,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'return_num'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
						id:'add',
						text:'选择借料单',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							openDialogResize("<%=contextPath %>/springViews/gd/chooseBorrow.jsp?callback=addRows&type=1&gd_id=<%=gd_id%>",1000, 800);
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
		});
		
		function addRows(ids,operation_dates,remarks){
			 if(ids==""){
				  return;
			 }
			 var idArray= new Array();
			 var operationDateArray= new Array();
			 var summaryArray= new Array();
			 idArray=ids.split(",");
			 operationDateArray=operation_dates.split(",");
			 summaryArray=remarks.split(",");
		     for (var i=0;i<idArray.length ;i++ )   
		     {
				$.ajax({
					type:'POST',
					async:false,
					url:'<%=contextPath %>/springMVC/gd/listGDCustomerBorrowDetail?borrow_id='+idArray[i],
					success:function(rtJson){
						for(var j=0;j<rtJson.length;j++){
							var return_num = 0;
							var return_money = 0;
							if(rtJson[j].return_num){
								return_num = rtJson[j].return_num;
							}
							if(rtJson[j].return_money){
								return_money = rtJson[j].return_money;
							}
							addRow(rtJson[j].id,operationDateArray[i],summaryArray[i],rtJson[j].warehouse_name,rtJson[j].code,rtJson[j].name,rtJson[j].format_type,rtJson[j].unit_name,rtJson[j].num,rtJson[j].cost,return_num,return_money);
						}
					}
				});
		     }
		}
		function addRow(borrow_detail_id,operation_date,summary,warehouse_name,code,name,format_type,unit_name,borrow_num,cost,already_return_num,already_return_money){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				borrow_detail_id:borrow_detail_id,
				operation_date:operation_date,
				summary:summary,
				warehouse_name:warehouse_name,
				code:code,
				name:name,
				format_type:format_type,
				unit_name:unit_name,
				borrow_num:borrow_num,
				return_money:'cost',
				already_return_num:already_return_num,
				return_num:borrow_num-already_return_num,
				already_return_money:already_return_money,
				return_money:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addGDMaterialRequest(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
				var products="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					if(jsondata.rows[i].return_num == null || jsondata.rows[i].return_num == "" || jsondata.rows[i].return_num <= 0){
						$.messager.alert('提示','第'+index+'行还料数量填写不正确！');
						return ;
					}
				}
				if(!checkNullOREmpty(products,'还料为必填项')){return;}
				var operation_date = $("#operation_date").datebox('getValue');
				var remark = encodeURIComponent($('#remark').val());
				
				var param = "type=1&gd_id=<%=gd_id%>&dept_id=<%=dept_id%>&dept_name=<%=dept_name%>&gd_code=<%=gd_code%>&gd_name=<%=gd_name%>&person_id=<%=person_id%>&person=<%=person%>&operation_date="+operation_date+"&remark="+remark+"&products="+products;
				$(obj).hide();
				$.ajax({
					type:'POST',
					<%=isAdd?"url:'"+contextPath+"/springMVC/gd/addGDCustomerReturn',":"url:'"+contextPath+"/springMVC/gd/updateGDCustomerReturn?return_id="+return_id+"',"%>
					data:param,
					dataType:'json',
					success:function(rtJson){
						if(rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}
						else{
							$.messager.alert("提示",rtJson.rtMsrg);
						}
						$(obj).show();
					}
				});
			}
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap>
						<input  id="gd_code" name="gd_code" readOnly value="<%=gd_code%>">
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap>
						<input  id="gd_name" name="gd_name" readOnly value="<%=gd_name%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly value="<%=dept_name%>">
					</td>
					<td align="right" class="td1" nowrap>申请人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>申请日期：</td>
					<td class="td2" nowrap colspan='3'>
						<input  id="operation_date" name="operation_date" class="easyui-datebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
						   <%if(!"1".equals(isShow)){ %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addGDMaterialRequest(this);">提交</a>
							<%} %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>